<html>
<head><meta charset="utf-8"><title>never type and enum sizes · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html">never type and enum sizes</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="200111161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/200111161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#200111161">(Jun 08 2020 at 15:08)</a>:</h4>
<p>I thought that using the never type in an enum would "cancel out" that variant completely, allowing the compiler to pack it tighter:</p>
<div class="codehilite"><pre><span></span><code><span class="cp">#![feature(never_type)]</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">Phase</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Expr</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Ident</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">enum</span> <span class="nc">Ast</span><span class="o">&lt;</span><span class="n">P</span>: <span class="nc">Phase</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">_Expr</span><span class="p">(</span><span class="n">P</span>::<span class="n">Expr</span><span class="p">,</span><span class="w"> </span><span class="kt">i32</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">_Ident</span><span class="p">(</span><span class="n">P</span>::<span class="n">Ident</span><span class="p">,</span><span class="w"> </span><span class="nb">String</span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Late</span><span class="p">(());</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Phase</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Late</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Expr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">!</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Ident</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="w"></span>
<span class="w">        </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">Ast</span><span class="o">&lt;</span><span class="n">Late</span><span class="o">&gt;&gt;</span><span class="p">(),</span><span class="w"></span>
<span class="w">        </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="nb">String</span><span class="o">&gt;</span><span class="p">(),</span><span class="w"></span>
<span class="w">    </span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>It doesn't seem to be the case:</p>
<div class="codehilite"><pre><span></span><code>assertion failed: `(left == right)`
  left: `32`,
 right: `24`&#39;
</code></pre></div>


<p>Am I incorrectly assuming that those should be the same size?</p>



<a name="200142079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/200142079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#200142079">(Jun 08 2020 at 19:03)</a>:</h4>
<p>Your expectation is wrong, unfortunately. See <a href="https://github.com/rust-lang/rust/issues/49298#issuecomment-380844923">https://github.com/rust-lang/rust/issues/49298#issuecomment-380844923</a> and the many issues linked to that issue</p>



<a name="200146740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/200146740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#200146740">(Jun 08 2020 at 19:45)</a>:</h4>
<p>This is why the layout of rust is not fixed. So that we can allow adding optimizations like this :-)</p>
<p>Anyway it seems that rust just fails to realize that optimization if the variant with <code>!</code> also contains a non zero-sized second field. Also the fact that <code>P</code> is generic could maybe play a role in making it harder to detect the optimization (through probably should not as resolving the types should be done before doing anything related to that kind of optimization).</p>



<a name="200146829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/200146829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#200146829">(Jun 08 2020 at 19:46)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=3c95b68bcbbf75bd2e39c94f5f9da990">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=3c95b68bcbbf75bd2e39c94f5f9da990</a></p>



<a name="200147260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/200147260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#200147260">(Jun 08 2020 at 19:50)</a>:</h4>
<p>I didn't saw what <span class="user-mention silent" data-user-id="124289">Hanna Kruppe</span>  had written. Which explains why it doesn't work for that specific case.</p>



<a name="200209912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/200209912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#200209912">(Jun 09 2020 at 11:23)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@Hanna Kruppe</span> thank you for the link (and your attempts to coalesce all the issues into one!). One thing I didn’t see is if leaving space is intended as a “final” / “desired” end state, or if it’s just to prevent bad code for now.</p>



<a name="200210198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/200210198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#200210198">(Jun 09 2020 at 11:26)</a>:</h4>
<p>I think that's here to stay, the desire to be able to eliminate temporaries won't go away. The specific comment I linked to seems to indicate this is also the T-compiler position.</p>



<a name="200210380"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/200210380" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#200210380">(Jun 09 2020 at 11:28)</a>:</h4>
<p>Interesting; one optimization preventing another. </p>
<p>With zero actual data to prove it, I’d feel like the space saving of the smaller enum would outweigh the one-time moving of temporaries.</p>



<a name="200516211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/200516211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#200516211">(Jun 11 2020 at 08:16)</a>:</h4>
<p>Cc <span class="user-mention" data-user-id="119009">@eddyb</span></p>



<a name="202036432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/202036432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#202036432">(Jun 25 2020 at 23:10)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> my reasoning is that temporaries are everywhere, whereas variants with an uninhabited <em>and</em> non-ZST data should be quite rare - rarer than <code>Result&lt;T, !&gt;</code>, anyway</p>



<a name="202037579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/202037579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#202037579">(Jun 25 2020 at 23:29)</a>:</h4>
<p>Perhaps I misunderstood which temporaries were being referred to. I was thinking it was temporaries when constructing the enum, but I'm now guessing that it's a broader scope.</p>



<a name="202038739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/202038739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#202038739">(Jun 25 2020 at 23:48)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/49298#issuecomment-380615281">https://github.com/rust-lang/rust/issues/49298#issuecomment-380615281</a> I assume</p>



<a name="202039124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/202039124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#202039124">(Jun 25 2020 at 23:55)</a>:</h4>
<p>If you think about the abstract semantics here, you get a temporary <code>String</code> returned from <code>String::new</code>, then the panic happens. The temporary is dropped. <code>x</code> never gets touched.</p>
<p>The optimization here is not to allocate space for the temporary on the stack, but to put it directly into the memory allocated for <code>x</code>. This overwrites the value previously in <code>x</code>, but it's okay, because the panic causes you to lose track of it anyway.</p>
<p>But if <code>Option&lt;(String, !)&gt;</code> is zero-sized, you can't do this optimization, because you'd actually write to the memory allocated for <code>y</code>. And that in turn means that you can't do this optimization for generic <code>Option&lt;(String, T)&gt;</code> until monomorphization when you can be confident that <code>T</code> is nonempty.</p>



<a name="202039722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/202039722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#202039722">(Jun 26 2020 at 00:02)</a>:</h4>
<p>That said, this might be a missed safe case of the optimization: even though the <code>_Expr</code> variant needs to have space for the <code>i32</code>, I don't think it needs space for a discriminant. You can never have a valid value with the <code>_Expr</code> variant.</p>



<a name="202040024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/202040024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#202040024">(Jun 26 2020 at 00:06)</a>:</h4>
<p>If it was the other way around, with <code>((), i32)</code> and <code>(!, String)</code> variants, you'd still need 3 words for the <code>String</code>, but again I don't think you need space for a discriminant. So this could still be size 24 and not 32.</p>



<a name="202041136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/202041136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#202041136">(Jun 26 2020 at 00:23)</a>:</h4>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> thoughts?</p>



<a name="202041295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/202041295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#202041295">(Jun 26 2020 at 00:25)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> in order to optimize away temporaries you'd have to check the layout or assume you can't (especially if generics are involved). I guess we could maybe special-case <code>enum</code> variants (but still have <code>(!, T)</code>be the size of <code>T</code>), with some work?</p>



<a name="202041393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/202041393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#202041393">(Jun 26 2020 at 00:27)</a>:</h4>
<p><span class="user-mention" data-user-id="123893">@alercah</span> reading now, seems like you said the same thing heh (in more detail)</p>



<a name="202041436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/202041436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#202041436">(Jun 26 2020 at 00:27)</a>:</h4>
<p><code>Option&lt;(!, T)&gt;</code> and similar are good examples because you might have non-ZST <code>(!, T)</code> that isn't stored in <code>Option</code> at all</p>



<a name="202041492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/202041492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#202041492">(Jun 26 2020 at 00:28)</a>:</h4>
<p>so basically you can't get rid of the temporary when constructing variants of enums. that still sounds pretty terrible</p>



<a name="202044085"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/202044085" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#202044085">(Jun 26 2020 at 01:27)</a>:</h4>
<p>ah that may well be that I am saying the same thing with more words :)</p>
<p>I'm not sure what you mean by "non-ZST <code>(!, T)</code> that isn't stored in <code>Option</code> at all"</p>



<a name="203352041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/203352041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#203352041">(Jul 09 2020 at 05:31)</a>:</h4>
<p><span class="user-mention" data-user-id="123893">@alercah</span> I meant that you could theoretically have a ZST <code>Option&lt;(!, T)&gt;</code> (no <code>Some</code> variant) while <code>(!, T)</code> remaining non-ZST (to support partial init)</p>



<a name="203352098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/203352098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#203352098">(Jul 09 2020 at 05:32)</a>:</h4>
<p>but then <code>Some(expr)</code> can't be made in-place unless you know the layout of <code>expr</code>'s type</p>



<a name="204968796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/204968796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#204968796">(Jul 24 2020 at 21:26)</a>:</h4>
<p>Hm, now that I think about it, is that not the case already though, because of niches? Without knowing the layout of <code>expr</code>'s type, you don't know whether it's allocated at offset 0 or 1 or 8.</p>



<a name="204968986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/204968986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#204968986">(Jul 24 2020 at 21:29)</a>:</h4>
<p>Unless the compiler takes a conservative approach to it pre-monomorphization by assuming there's no niche, allocating enough extra space to store the discriminant just in case (to avoid going off the end), and then letting monomorphization figure out where the discriminant actually goes and what the actual object address is.</p>



<a name="204969140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/204969140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#204969140">(Jul 24 2020 at 21:31)</a>:</h4>
<p>I don't know anything about the workings of the compiler, but it seems to me it would at the very least need to know the byte offset of <code>expr</code> in <code>Option&lt;T&gt;</code> pre-monomorphization to avoid something ridiculous like that.</p>



<a name="204969303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/204969303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#204969303">(Jul 24 2020 at 21:33)</a>:</h4>
<p>If I assume that's the case, and the pre-monomorphization code has some way to know the byte offset <code>b</code> of the <code>T</code> in <code>Some(T)</code>, say as a <code>usize</code>, then perhaps that could be extended by sending a <code>Option&lt;usize&gt;</code> and, if <code>b.is_none()</code>, then the pre-monomorphization code will know that <code>expr</code> will not fit into the object, forcing a temporary stack allocation.</p>



<a name="204970445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/204970445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#204970445">(Jul 24 2020 at 21:49)</a>:</h4>
<p><span class="user-mention" data-user-id="119009">@eddyb</span></p>



<a name="204971960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/204971960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#204971960">(Jul 24 2020 at 22:11)</a>:</h4>
<p>The offset isn't known before monomorphization, but that isn't a problem: as long as it'll be <em>some</em> offset (as opposed to rerouting to an entirely different location), it's just as easy to compute it at monomorphization time. Note that this isn't really enum specific, e.g. even with <code>#[repr(C)] struct Pair&lt;T&gt;(T, T)</code> you can't know the offset of the second field without knowing what's substituted for T.</p>



<a name="204972462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/204972462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#204972462">(Jul 24 2020 at 22:18)</a>:</h4>
<p>(And on that note it still doesn't make sense that <code>core</code> doesn't have an offset_of macro)</p>



<a name="204981497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/204981497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#204981497">(Jul 25 2020 at 01:23)</a>:</h4>
<p>I guess you could add it now that raw references exist.</p>



<a name="205073748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/never%20type%20and%20enum%20sizes/near/205073748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/never.20type.20and.20enum.20sizes.html#205073748">(Jul 27 2020 at 00:33)</a>:</h4>
<p>Hm, I guess in order to really understand it I'd need to look at the compiler code, wouldn't I?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>